package com.applink.sell_proof.database.table;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.EMarketingContentProvider;

public class AnalyticDBHelper {

	public static final String TABLE_NAME = "data_analytic";
	public static final String ID = "record_id";
	public static final String RECORD_TAG = "record_tag";
	public static final String RECORD_TIME = "record_time";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ ID + " text primary key ," + Constant.ACCOUNT_ID + " text ,"
			+ RECORD_TAG + " text ," + RECORD_TIME
			+ " timestamp default (strftime('%s', 'now'))" + ");";

	public static void tracked(final String tag) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues values = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();

				String accountid = EMarketing.getInstance().getAccountId();

				if ((accountid == null) || (accountid.equals("")))
					return;

				values.put(ID, UUID.randomUUID().toString());
				values.put(RECORD_TAG, tag);
				values.put(Constant.ACCOUNT_ID, accountid);

				long time = System.currentTimeMillis();
				values.put(RECORD_TIME, time);

				resolver.insert(
						EMarketingContentProvider.ANALYTICDATA_CONTENT_URI,
						values);
			}

		}.start();
	}

//	public static void tracked() {
//
//		StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
//
//		String TAG = ste.getFileName() + "." + ste.getMethodName();
//
//		tracked(TAG);
//	}

	public static ConcurrentHashMap<String, HashMap<String, Object>> getAllRecords() {

		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();

		// select all rows and
		Cursor cursor = resolver.query(
				EMarketingContentProvider.ANALYTICDATA_CONTENT_URI, null, null,
				null, RECORD_TIME + " DESC");

		if (cursor != null) {

			ConcurrentHashMap<String, HashMap<String, Object>> records = new ConcurrentHashMap<String, HashMap<String, Object>>();

			while (cursor.moveToNext()) {

				try {
					HashMap<String, Object> record = new HashMap<String, Object>();

					record.put(Constant.ACCOUNT_ID, cursor.getString(cursor
							.getColumnIndex(Constant.ACCOUNT_ID)));

					record.put(RECORD_TAG,
							cursor.getString(cursor.getColumnIndex(RECORD_TAG)));

					record.put(RECORD_TIME,
							cursor.getLong(cursor.getColumnIndex(RECORD_TIME)));

					String id = cursor.getString(cursor.getColumnIndex(ID));

					record.put(ID, id);

					records.put(id, record);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
			}

			return records.size() > 0 ? records : null;
		}
		return null;
	}

	public static void emptyTable() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();

		// select all rows and
		resolver.delete(EMarketingContentProvider.ANALYTICDATA_CONTENT_URI,
				null, null);
	}
}
